<?php
/**
 * Created by PhpStorm.
 * User: happy
 * Date: 19/4/15
 * Time: 下午10:05
 */
//放在
class inotifyPlugin extends Plugin{
    //作为docker主进程,不退出
    public  function  docker_start(){
        $file_path='start.log';
        $sleep_interval='1';
        $log_path='data/log/inotify.log';

        if(!file_exists($file_path)){
            echo chr(10).$file_path.' not exist!';
        }

        $inotify_pid=posix_getpid();
        $start_alter_stamp=filemtime($file_path);
        $master_id=$this->get_swoole_master_id();

        if(!$master_id){
            echo chr(10).'swoole server is not start!';
            return false;}

        file_put_contents($file_path,$master_id);

        $pre_content=$master_id;//这个就是master_id的值,会写入$pre_content
        $msg=chr(10).'inotify start,last alter time:'.date('Y-m-d H:i:s',$start_alter_stamp).' master_pid:'.$pre_content.' ;inotify_pid:'.$inotify_pid;

        write($msg,$log_path);
        echo $msg;
        //每次都是重新启动
        while(true){
            sleep($sleep_interval);
            $now_content=trim(file_get_contents($file_path));
            //echo chr(10).$pre_content.'===>'.$now_content;
            if($now_content=='reload'||$now_content=='r'||$now_content=='R'){
                $time1=microtime(true);
                file_put_contents($file_path,'server reloading');
                $master_id=$this->get_swoole_master_id();
                if(!$master_id){
                    write('[reload]inotify diff change but no master id:'.$pre_content.'===>'.$now_content,$log_path);
                    continue;
                }
                write('[reload]inotify diff and reload worker '.$master_id.':'.$pre_content.'===>'.$now_content,$log_path);
                //kill -15 主进程PID*/
                $time1=microtime(true);
                $cmd='kill -USR1 '.$master_id;
                // write($cmd);
                exec($cmd);//重启所有worker进程
                mk_dir('data/log');
                $time2=microtime(true);
                $diff1=($time2-$time1)*1000;
                write('[reload] reload use time:'.$diff1.'ms',$log_path);
                $new_master_pid=$this->get_swoole_master_id();
                file_put_contents($file_path,$new_master_pid);
            }
            //s或者S代表重启服务器程序
            elseif($now_content=='start'||$now_content=='s'||$now_content=='S'){

                $master_id=$this->get_swoole_master_id();
                if(!$master_id){
                    //重新启动
                    file_put_contents($file_path,'restart');
                    $cmd='php start.php >> data/log/swoole_reload.log 2>&1 &';//重启后也会自动启动新的监控进程
                    exec($cmd,$output);

                    write('[restart]inotify find no master id and restart:'.$pre_content.'===>'.$now_content,$log_path);
                    continue;
                }
                   write('[restart]inotify diff and restart master '.$master_id.':'.$pre_content.'===>'.$now_content,$log_path);
                //kill -15 主进程PID*/
                $time1=microtime(true);
                $cmd='kill -15 '.$master_id;
               // write($cmd);
                exec($cmd);
                $time2=microtime(true);
                mk_dir('data/log');
                $cmd='php start.php >> data/log/swoole_reload.log 2>&1 &';//重启后也会自动启动新的监控进程
                exec($cmd,$output);

                $time3=microtime(true);
                $diff1=($time2-$time1)*1000;
                $diff2=($time3-$time2)*1000;
                write('[restart] stop use time:'.$diff1.'ms,start use time:'.$diff2.'ms',$log_path);
                $new_master_pid=$this->get_swoole_master_id();
                file_put_contents($file_path,$new_master_pid);
            }

        }
    }


    //获取swoole_master id,通过swoole plugin 中设置的主进程名包含swoole_master获取第一个即可
    protected  function  get_swoole_master_id(){
        $cmd = 'ps aux |grep swoole_master';//ps aux |grep master
        exec($cmd, $out);
        if (is_array($out)) {
            $master=$out[0];//第一个最小的id为master的进程
          //  write($master);
            if($master){
                $id_arr= explode(' ',$master);
                foreach($id_arr as $value){
                    $value=trim($value);
                    if($value&&is_numeric($value)&&$value>1){
                        return $value;
                    }
                }
                $mater_pid=$id_arr[8];
                return $mater_pid;
            }
        }
        return false;
    }
}